package com.wgts.wgts.react.update;

import android.app.DownloadManager;
import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;

import com.wgts.wgts.react.ReactConstant;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;

import static com.wgts.wgts.react.ReactConstant.JS_ENTY;
import static com.wgts.wgts.react.update.ToolsUtil.deleteFile;

public class ProduceVersion {

    private static final String TAG = "ProduceVersion";


    /**
     *
     * 文件路径不允许后面有"/"
     * 1.进入下载目录， 校验下载文件的MD5
     * 2.解压下载目录下的zip文件，并静文件放入解压目录下
     * 3.校验解压后的文件， 校验js文件，校验资源文件
     * 4.将文件重新移入JS对应的版本文件夹中
     *
     *
     *
     *
     *
     *
     *
     *
     *
     * @param downPath 下载文件位置
     * @param
     * @param MD5 校验码
     * @param destPath 操作结束后将更新的版本放入哪个文件夹下（不包括版本号， 即所有版本保存的文件夹）
     * @param jsVersion 本次更新版本号
     */
    public static void decompression(final String downPath,  final String MD5, final String destPath, final String jsVersion) {

        new Thread(new Runnable() {

            String tmpPath = destPath + File.separator + jsVersion + ReactConstant.TMP_FILE;

            String destVersionPath = destPath + File.separator + jsVersion;

            @Override
            public void run() {
                File dest = new File(tmpPath);

                if(dest.exists()){
                    dest.delete();
                }

                Log.d(TAG, "===================================\n" +   "下载完成\n" + "===================================");

                if(ToolsUtil.checkZip(MD5, downPath)){
                    Log.d(TAG, "===================================\n"  + "zip校验完成\n" + "===================================");

                    ToolsUtil.unzip(downPath, tmpPath);
                    File src = new File(downPath);
                    try {
                        ToolsUtil.copyFolder(src, dest);
                        if(!ToolsUtil.CheckDetail(tmpPath, JS_ENTY)){
                            deleteFile(new File(tmpPath));
                            Log.d(TAG, "===================================\n"  + "清单校验完成\n" + "===================================");

                        }else{
                            boolean isSuccess = dest.renameTo(new File(destVersionPath));
                            if(!isSuccess){
                                Log.d(TAG, "===================================\n"  + "重命名失败\n" + "===================================");
                                dest.delete();
                            }
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                        Log.e(TAG, "===================================\n"  + "出错了\n" + e.getMessage() + "\n" + "===================================");

                        deleteFile(new File(tmpPath));
                        deleteFile(src);
                    }
                    deleteFile(src);

                }
                File file = new File(downPath);
                file.delete();

            }
        }).start();
    }

    /**
     *
     *
     * 1.进入下载路径，校验ZIP的MD5
     * 2.解压下载zip文件并将文件放入解压文件夹
     * 3.拷贝基本版本到临时文件。
     * 4.合并基本版本JS文件，合并基本版本资源文件
     * 5.校验合并后版本JS文件，检验合并后资源文件
     * 6.将合并后版本重命名到版本存放位置
     *
     *
     *
     *
     * @param destPath 操作完成后保存的JS版本的文件夹（不包括版本号， 即所有版本保存的文件夹）
     * @param downloadPath 下载路径
     * @param unZipPath 解压路径
     * @param MD5 版本号
     * @param baseVersionPath 增量基于某个版本的路径
     * @param pachResourcePath 增量资源文件
     * @param patchJSName 增量文件名称
     * @param jsVersion 本次更新版本号
     */
    public static void merge(final String destPath, final String downloadPath, final String unZipPath, final String MD5, final String baseVersionPath, final String pachResourcePath, final String patchJSName, final String jsVersion){

        new Thread(new Runnable() {
            @Override
            public void run() {

                String tmpPath = destPath +  File.separator + jsVersion + ReactConstant.TMP_FILE;

                String destVersionPath = destPath +  File.separator + jsVersion;

                File unzip = new File(unZipPath);

                File dest = new File(tmpPath);

                if(unzip.exists()){
                    unzip.delete();
                }

                if(dest.exists()){
                    dest.delete();
                }

                Log.d(TAG, "===================================\n"  + "增量下载完成\n" + "===================================");

                if(ToolsUtil.checkZip(MD5, downloadPath)){
                    Log.d(TAG, "===================================\n"  + "zip增量MD5校验完成\n" + "===================================");

                    ToolsUtil.unzip(downloadPath, unZipPath);
                    Log.d(TAG, "===================================\n"  + "zip增量解压完成\n" + "===================================");

                    File src = new File(baseVersionPath);
                    try {
                        ToolsUtil.copyFolder(src, dest);
                    } catch (IOException e) {
                        e.printStackTrace();

                    }
                    ToolsUtil.mergePatAndAsset(tmpPath + File.separator + JS_ENTY, unZipPath + File.separator + patchJSName, pachResourcePath, tmpPath);

                    //重命名文件
                    try {
                        if(!ToolsUtil.CheckDetail(tmpPath, JS_ENTY)){
                            deleteFile(new File(tmpPath));
                            Log.d(TAG, "===================================\n"  + "Detail文件校验失败\n" + "===================================");

                        }else{
                            boolean isSucess = dest.renameTo(new File(destVersionPath));
                            if(!isSucess){
                                Log.d(TAG, "===================================\n"  + "重命名失败\n" + "===================================");
                                dest.delete();
                            }
                        }
                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                        Log.d(TAG, "===================================\n"  + e.getMessage() + "===================================");
                        deleteFile(new File(tmpPath));

                    }
                }
                File file = new File(downloadPath);
                file.delete();
                File file1 = new File(unZipPath);
                file1.delete();

            }
        }).start();
    }


}
